package com.tudulist.service.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sun.jersey.api.core.InjectParam;
import com.tudulist.dao.TodoListDao;
import com.tudulist.dao.UserDao;
import com.tudulist.model.TodoList;
import com.tudulist.model.User;
import com.tudulist.rest.resource.TodoListResource;

@Component
@Path("/todolist")
public class TodoListResourceServer implements TodoListResource {

	@InjectParam
	TodoListDao todoListDao;

	@InjectParam
	UserDao userDao;

	@POST
	@Path("/add")
	@Consumes(MediaType.APPLICATION_XML)
	@Transactional
	public TodoList createTodoList(@HeaderParam("username") String username, TodoList todoList) {//FIXME share security context
		User user = userDao.find(username);
		todoList.setUser(user);
		return todoListDao.save(todoList);
	}

	@GET
	@Path("/findall/{username}")
	@Produces(MediaType.APPLICATION_XML)
	@Transactional
	public List<TodoList> findAll(@PathParam("username") String username) {//FIXME share security context
		User user = userDao.find(username);
		return todoListDao.findAll(user);
	}

	@GET
	@Path("{todolistId}")
	@Produces(MediaType.APPLICATION_XML)
	@Transactional
	public TodoList findById(@PathParam("todolistId") Long todolistId) {
		return todoListDao.find(todolistId);
	}

}
